*Analysis of regional concentration

use regressionsdataset, clear

*keep necessary variables
keep tkcarrier market opcarrier1 opcarrier2 opcarrier3 year quarter mktpass_tkopcarrier owned_regional1 unowned_regional1 owned_regional2 unowned_regional2 owned_regional3 unowned_regional3

*generate a count variable to keep track of routes at the tkcarrier-market level to prevent overcounting of regionals who operate multiple segments in a route
bysort tkcarrier market year quarter: generate route = _n 

*reshape long so that data is at major-regional-market-time level
keep tkcarrier market year quarter mktpass_tkopcarrier route opcarrier1 owned_regional1 unowned_regional1

*keep only the regional observations
keep if owned_regional1 == 1 | unowned_regional1 == 1

rename opcarrier1 opcarrier
rename owned_regional1 owned_regional
rename unowned_regional1 unowned_regional

*save temp file
save temp1, replace

use regressionsdataset, clear

*keep necessary variables
keep tkcarrier market opcarrier1 opcarrier2 opcarrier3 year quarter mktpass_tkopcarrier owned_regional1 unowned_regional1 owned_regional2 unowned_regional2 owned_regional3 unowned_regional3

*generate a count variable to keep track of routes at the tkcarrier-market level to prevent overcounting of regionals who operate multiple segments in a route
bysort tkcarrier market year quarter: generate route = _n 

*reshape long so that data is at major-regional-market-time level
keep tkcarrier market year quarter mktpass_tkopcarrier route opcarrier2 owned_regional2 unowned_regional2

*keep only the regional observations
keep if owned_regional2 == 1 | unowned_regional2 == 1

rename opcarrier2 opcarrier
rename owned_regional2 owned_regional
rename unowned_regional2 unowned_regional

save temp2, replace

use regressionsdataset, clear

*keep necessary variables
keep tkcarrier market opcarrier1 opcarrier2 opcarrier3 year quarter mktpass_tkopcarrier owned_regional1 unowned_regional1 owned_regional2 unowned_regional2 owned_regional3 unowned_regional3

*generate a count variable to keep track of routes at the tkcarrier-market level to prevent overcounting of regionals who operate multiple segments in a route
bysort tkcarrier market year quarter: generate route = _n 

*reshape long so that data is at major-regional-market-time level
keep tkcarrier market year quarter mktpass_tkopcarrier route opcarrier3 owned_regional3 unowned_regional3

*keep only the regional observations
keep if owned_regional3 == 1 | unowned_regional3 == 1

rename opcarrier3 opcarrier
rename owned_regional3 owned_regional
rename unowned_regional3 unowned_regional

append using temp1
append using temp2
erase temp1.dta
erase temp2.dta

sort market year quarter tkcarrier route

*drop duplicates from regionals operating multiple segments in a route
duplicates drop tkcarrier market opcarrier year quarter mktpass_tkopcarrier route, force

*some passengers for regionals across different routes
bysort tkcarrier opcarrier market year quarter: egen regional_major_passengers = sum(mktpass_tkopcarrier)

*this results in duplicate entries at the tkcarrier-opcarrier-market level
keep tkcarrier opcarrier market year quarter owned_regional unowned_regional regional_major_passengers
duplicates drop

*sum up total number of passengers carried by regionals across all majors in market
bysort market opcarrier year quarter: egen regional_passengers = sum(regional_major_passengers)
drop regional_major_passengers tkcarrier
duplicates drop

*keep only independent regionals
drop if owned_regional == 1

*analysis of regional HHI and national level by year
save temp, replace

*sum up passengers by regionals for each year across all markets
bysort year quarter opcarrier: egen total_reg_pass = sum(regional_passengers)

keep opcarrier year quarter total_reg_pass
duplicates drop

*sum total regional passengers in each year
bysort year quarter: egen total_passengers = sum(total_reg_pass)

gen national_mkt_share = total_reg_pass / total_passengers

gen national_hhi_step = national_mkt_share^2

bysort year quarter: egen national_hhi = sum(national_hhi_step)
keep year quarter national_hhi total_passengers
duplicates drop

*analysis at market-level
use temp, clear

keep market opcarrier year quarter regional_passengers

bysort market year quarter: egen total_passengers = sum(regional_passengers)

gen regional_mkt_share = regional_passengers / total_passengers 

gen regional_mkt_hhi_step = regional_mkt_share^2 

bysort market year quarter: egen regional_mkt_hhi = sum(regional_mkt_hhi_step)
keep market year quarter regional_mkt_hhi
duplicates drop

sort year quarter market 

*save market level data
save market_level_regional_hhi_data.dta, replace






